﻿using UnityEngine;
using System.Collections;

public class follower : MonoBehaviour {
	//the relative rotation our gameobject should have with respect to the leader.
	public float relativeYRotation;
	//the relative scale of the distance behind our leader in x and z.
	public float relativeXPositionScale;
	public float relativeZPositionScale;

	//a reference to our target position
	public Vector3 target;
	//save a reference to our final target after a collision occurs
	public Vector3 finalTarget;

	//a reference to our leaders game object
	public GameObject leader;
	//a reference to the leaders script
	public KinematicArrive leaderScript;

	//The maximum speed our object may travel
	public float maxSpeed = .1f;
	//The radius around our target destination that is considered close enough that we have arrived.
	public float radius;
	//the time to target constant
	public float timeToTarget = .1f;
	public bool isMoving = false;
	
	public bool colliding;
	public int numCollisions;

	// Use this for initialization
	void Start () {
		//initialize the target position, radius of satisfaction, and the maximum speed multiplier
		target = transform.position;
		radius = .4f;
		maxSpeed = 12f;
		numCollisions = 0;
	}
	
	// Update is called once per frame
	void Update () {
		//rotation logic.
		float leaderYRotation = leader.transform.rotation.eulerAngles.y + relativeYRotation;
		transform.rotation = Quaternion.AngleAxis( leaderYRotation, Vector3.up);

		//positional logic
		target = leader.transform.position - leader.transform.forward * relativeZPositionScale + leader.transform.right * relativeXPositionScale;
		//if you collide with an object, go around it
		if(colliding){
			target = transform.position + (transform.right * 10);
		}
		KinematicSteeringOutput Steering = steering ();
		transform.position += Steering.velocity * Time.deltaTime/2;
	}

	public KinematicSteeringOutput steering(){
		//create the structure for the output
		KinematicSteeringOutput steering = new KinematicSteeringOutput();
		
		// get the direction to the target
		steering.velocity = target - transform.position;
		// if we are within the radius, we can return false
		if (steering.velocity.magnitude < radius){
			KinematicSteeringOutput emptySteering = new KinematicSteeringOutput();
			isMoving = false;
			return emptySteering;
		}
		
		//otherwise, we need to move to our target
		//get there in timeToTarget seconds
		steering.velocity /= timeToTarget;
		
		//see if this is greater than our max speed, if it is, set it equal to max speed.
		if (steering.velocity.magnitude > maxSpeed){
			steering.velocity.Normalize();
			steering.velocity *= maxSpeed;
		}

		//return
		steering.rotation = new Vector3(0,0,0);
		return steering;
	}

	void OnTriggerEnter(Collider other){
		if(other.gameObject.CompareTag("obstacle")){
			colliding = true;
			numCollisions++;
		}
	}
	
	void OnTriggerExit(Collider other){
		if(other.gameObject.CompareTag("obstacle")){
			numCollisions--;
			if(numCollisions==0)
				colliding = false;
		}
	}
}